package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.dto.StudentQuizDto;
import com.jyy.prefabricated.entity.Student;
import com.jyy.prefabricated.entity.StudentQuiz;
import com.jyy.prefabricated.service.StudentQuizService;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.CoursePackageQuizVo;
import com.jyy.prefabricated.vo.StudentQuizVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生随堂考管理表(StudentQuiz)表控制层
 *
 * @author makejava
 * @since 2021-10-11 13:49:02
 */
@RestController
@RequestMapping("/api")
public class StudentQuizController {
    /**
     * 服务对象
     */
    @Resource
    private StudentQuizService studentQuizService;

    @Resource
    private UserUtils userUtils;

    /**
     * 我的随堂考列表
     *
     * @param dto 主键
     * @return 单条数据
     */
    @GetMapping("/student-quiz-my")
    public AjaxResult selectMyQuiz(StudentQuizDto dto) {
        Student student = userUtils.getCurrentStudent();

        dto.setStudentId(student.getId());
        List<StudentQuizVo> list = studentQuizService.selectMyQuiz(dto);

        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<StudentQuizVo> studentQuizVoList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (StudentQuizVo item : list) {
                if (item.getPurchaseCount() != null && item.getPurchaseCount() > 0) {
                    studentQuizVoList.add(item);
                }
            }
        }
        PageInfo<StudentQuizVo> result = new PageInfo<>(studentQuizVoList);
        return AjaxResult.success(result);
    }

    /**
     * 我的随堂考详情不分页
     *
     * @param categoryId 主键
     * @return 单条数据
     */
    @GetMapping("/student-quiz/{categoryId}")
    public AjaxResult selectByCategoryId(@PathVariable Integer categoryId) {
        Student student = userUtils.getCurrentStudent();
        List<CoursePackageQuizVo> list = studentQuizService.selectByCategoryIdAndStudentId(student.getId(), categoryId);
        return AjaxResult.success(list);
    }

    /**
     * 我的随堂考详情分页
     *
     * @param categoryId 主键
     * @return 单条数据
     */
    @GetMapping("/student-quiz-list/{categoryId}")
    public AjaxResult selectByCategoryId(@PathVariable Integer categoryId, Pageable pageable) throws IOException, NoSuchAlgorithmException {
        Student student = userUtils.getCurrentStudent();
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CoursePackageQuizVo> list = studentQuizService.selectByCategoryIdAndStudentId(student.getId(), categoryId);
        PageInfo<CoursePackageQuizVo> result = new PageInfo<>(list);
        return AjaxResult.success(list);
    }

    /**
     * 我的随堂考详情
     *
     * @param coursePackageId 課程包id
     * @return 单条数据
     */
    @GetMapping("/can-quiz/{coursePackageId}")
    public AjaxResult selectByCoursePackageId(@PathVariable Integer coursePackageId) {
        Student student = userUtils.getCurrentStudent();
//        PageHelper.startPage(1, 20);
        List<CoursePackageQuizVo> list = studentQuizService.selectByCoursePackageId(student.getId(), coursePackageId);
//        PageInfo<CoursePackageQuizVo> result = new PageInfo<>(list);
        return AjaxResult.success(list);
    }

    /**
     * 开始练习
     *
     * @param dto
     * @return
     */
    @PostMapping("/student-quiz")
    public AjaxResult insert(@Valid @RequestBody StudentQuizDto dto) {
        Student student = userUtils.getCurrentStudent();
        dto.setStudentId(student.getId());
        Integer id = studentQuizService.insert(dto);
        StudentQuiz result = studentQuizService.queryById(id);
        return AjaxResult.success(result);
    }

    /**
     * 交卷
     *
     * @param studentQuizId
     * @return
     */
    @PutMapping("/student-quiz/{studentQuizId}")
    public AjaxResult finish(@PathVariable Integer studentQuizId) {

        studentQuizService.finishQuiz(studentQuizId);
        return AjaxResult.success();
    }


}
